package officeboost.domain.equip_venue;

import java.util.ArrayList;
import java.util.GregorianCalendar;

public abstract class ReservableItem {

    private String name;
    private String usage;
    private String status;
    public ArrayList<ReservationXH> periodList = new ArrayList<ReservationXH>();
    private int reserveIdNum = 1;

    public String getUsage() {
	return usage;
    }

    public void setUsage(String useage) {
	this.usage = useage;
    }

    public void setName(String name) {
	this.name = name;
    }

    public void setStatus(String status) {
	this.status = status;
    }

    public String getStatus() {
	return status;
    }

    public String getName() {
	return name;
    }

    private void addReserve(GregorianCalendar start, GregorianCalendar end) {
	ReservationXH addition = new ReservationXH();
	addition.setStartTime(start);
	addition.setEndTime(end);
	addition.setReserveId(reserveIdNum);
	reserveIdNum = reserveIdNum + 1;

	if (periodList.isEmpty()) {
	    periodList.add(addition);
	} else {
	    int i = 0;
	    boolean bHaveFound = false;
	    while (!bHaveFound) {
		if (addition.getStartTime().before(periodList.get(i))) {
		    periodList.add(i, addition);
		    bHaveFound = true;
		}
		i = i + 1;
	    }
	}

    }

    public boolean ableToReserve(GregorianCalendar tempStart, GregorianCalendar tempEnd) {
	boolean able = true;
	if (periodList.isEmpty()) {
	    able = true;
	} else {
	    for (int i = 0; i < periodList.size(); i++) {
		if ((tempStart.after(periodList.get(i).getStartTime()) && tempStart.before(periodList.get(i).getEndTime())) || tempEnd.after(periodList.get(i).getStartTime()) && tempEnd.before(periodList.get(i).getEndTime())) {
		    able = false;
		}
	    }
	}
	return able;

    }

    public boolean reserve(GregorianCalendar startTime, GregorianCalendar endTime) {
	boolean result = false;


	if (startTime.after(endTime)) {
	    GregorianCalendar temp = new GregorianCalendar();
	    temp = startTime;
	    startTime = endTime;
	    endTime = temp;
	}

	result = ableToReserve(startTime, endTime);
	if (result) {
	    addReserve(startTime, endTime);
	    this.status = "Reserved";
//			System.out.println("You have successfully reserved.");
//			System.out.println("Start time:"+startTime.get(Calendar.YEAR)+"��"+ startTime.get(Calendar.MONTH)+"��"+startTime.get(Calendar.DAY_OF_MONTH)+"��"+startTime.get(Calendar.HOUR)+"ʱ"+startTime.get(Calendar.MINUTE)+"��");
//			System.out.println("End time:"+endTime.get(Calendar.YEAR)+"��"+ endTime.get(Calendar.MONTH)+"��"+endTime.get(Calendar.DAY_OF_MONTH)+"��"+endTime.get(Calendar.HOUR)+"ʱ"+endTime.get(Calendar.MINUTE)+"��");
//			System.out.println("Reserve ID: "+ (reserveIdNum-1));
//			System.out.println("");
	} else {
//			System.out.println("Sorry, this period of time has been reserved");
	}
	return result;
    }

    public boolean cancelReserve(int id) {

//		this.periodList.remove(0);
//		//System.out.println("Cancelled ID: "+ id);
//		this.status = "Available";

	int i = 0;
	boolean isFound = false;
	boolean isAllSearched = false;
	while (!isFound && !isAllSearched) {
	    if (this.periodList.get(i).getReserveId() == id) {
		this.periodList.remove(i);
		isFound = true;
	    }
	    if (i == (this.periodList.size() - 1)) {
		isAllSearched = true;
//		System.out.println("The wrong reserve ID: " + id);
	    }
	    i++;
	}
	
	return isFound;

    }

    public void release() {
	this.periodList.remove(0);
	this.status = "Available";

    }

    public void use() {
	this.status = "Using";
    }
}
